package o2sjsc.core.libs;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public class PrefsManager {
	/**
	 * Filename
	 */
	public static final String PREFS_NAME = "settings";

	private PrefsManager instance;
	private SharedPreferences preferences;

	/**
	 * Contractor
	 * 
	 * @param context
	 */
	public PrefsManager(Context context) {
		preferences = PreferenceManager.getDefaultSharedPreferences(context);

		/*
		 * preferences = context.getSharedPreferences(PREFS_NAME,
		 * Context.MODE_PRIVATE);
		 */
	}

	/**
	 * Use to initialization and get object AppSettings
	 * 
	 * @param context
	 */
	public PrefsManager getInstance(Context context) {
		if (instance == null) {
			instance = new PrefsManager(context);
		}

		return instance;
	}

	/**
	 * Set an int value in the preferences editor
	 * 
	 * @param key
	 *            : name of preference to modify
	 * @param value
	 *            : the new value of preference
	 * @return : AppSettings to continue edit
	 */
	public PrefsManager putInt(String key, int value) {
		preferences.edit().putInt(key, value).commit();
		return instance;
	}

	/**
	 * Retrieve an int value from the preferences.
	 * 
	 * @param key
	 *            : the name of preference to retrieve
	 * @param defaultValue
	 *            : value to return if this preference does not exist
	 * @return : return the preference value if it exist, else return default
	 *         value
	 */
	public int getInt(String key, int defaultValue) {
		return preferences.getInt(key, defaultValue);
	}

	/**
	 * Set a long value in preference editor
	 * 
	 * @param key
	 *            : name of preference to modify
	 * @param value
	 *            : the new value of preference
	 * @return : AppSettings to continue edit
	 */
	public PrefsManager putLong(String key, long value) {
		preferences.edit().putLong(key, value).commit();
		return instance;
	}

	/**
	 * Retrieve an long value from the preferences
	 * 
	 * @param key
	 *            : the name of preference to retrieve
	 * @param defaultValue
	 *            : value to return if this preference does not exist
	 * @return : return the preference value if exist, else return default value
	 */
	public long getLong(String key, long defaultValue) {
		return preferences.getLong(key, defaultValue);
	}

	/**
	 * Set a String value in preference editor
	 * 
	 * @param key
	 *            : name of preference to modify
	 * @param value
	 *            : the new value of preference
	 * @return : return AppSettings to continue edit
	 */
	public PrefsManager putString(String key, String value) {
		preferences.edit().putString(key, value).commit();
		return instance;
	}

	/**
	 * Retrieve a String value from the preference
	 * 
	 * @param key
	 *            : the name of preference to retrieve
	 * @param defaultValue
	 *            : value to return if preference does not exist
	 * @return : return the preference value if exist, else return default value
	 */
	public String getString(String key, String defaultValue) {
		try {
			return preferences.getString(key, defaultValue);
		} catch (Exception exception) {
			return exception.getMessage();
		}
	}
	
	public String getString(Context context,String key, String defaultValue) {
		try {
			return preferences.getString(key, defaultValue);
		} catch (Exception exception) {
			return exception.getMessage();
		}
	}

	/**
	 * Set a boolean value in preference editor
	 * 
	 * @param key
	 *            : name of preference to modify
	 * @param value
	 *            : the new value of preference
	 * @return : return AppSettings to continue edit
	 */
	public PrefsManager putBoolean(String key, boolean value) {
		preferences.edit().putBoolean(key, value).commit();
		return instance;
	}

	/**
	 * Retrieve a boolean value from the preferences
	 * 
	 * @param key
	 *            : the name of preference to retrieve
	 * @param defaultValue
	 *            : value to return if preference does not exist
	 * @return : return the preference value if exist, else return default value
	 */
	public boolean getBoolean(String key, boolean defaultValue) {
		return preferences.getBoolean(key, defaultValue);
	}

	/**
	 * Set a float value in preference editor
	 * 
	 * @param key
	 *            : name of preference to modify
	 * @param value
	 *            : the new value of preference
	 * @return : return AppSettings to continue edit
	 */
	public PrefsManager putFloat(String key, float value) {
		preferences.edit().putFloat(key, value).commit();
		return instance;
	}

	/**
	 * Retrieve a float value from the preferences
	 * 
	 * @param key
	 *            : the name of preference to retrieve
	 * @param defaultValue
	 *            : value to return if preference dose not exist
	 * @return : return the preference value if exist, else return default value
	 */
	public float getFloat(String key, float defaultValue) {
		return preferences.getFloat(key, defaultValue);
	}
}